/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundless.managedbeans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import boundless.roster.*;
import boundless.entities.*;
import boundless.ejb.*;
import java.util.List;
import javax.ejb.EJB;

/**
 *
 * @author hendrikdekock
 */
@ManagedBean
@SessionScoped
public class RosterBean {

    @Inject
    private Fantasyteams team;
    
    @EJB
    private RequestBean rb;
    
    private Roster roster;

    public static final int NUM_BENCHED_ALLOWED = 6;
    
    String error;
    boolean canAssign;
    
    /**
     * Creates a new instance of RosterBean
     */
    public RosterBean() {
    }
    
    
    public void makeRoster() {
        List<Playersandpositionsbyteam> entries = rb.getFantasyTeamRoster(team);
        roster = new Roster(team.getId());
        for (int i = 0; i < entries.size(); i++) {
            Playersandpositionsbyteam entrant = entries.get(i);
            roster.addNode(entrant.getId(), entrant.getPlayposition(), true);
        }
    }
    
    
    /**
     * Validates the roster as following Fantasy Football rues
     * @return True if the Roster is valid, False, otherwise
     */
    public boolean validateRoster() {
        int numBenched = roster.numBenched();
        if (numBenched > NUM_BENCHED_ALLOWED) {
            error = "Too many benched players";
            return false;
        }
        if (numBenched < NUM_BENCHED_ALLOWED) {
            error = "Too many active players";
            return false;
        }
        Playposition cur;
        int min, max, num;
        for (int i = Playposition.QUARTER_BACK; i <= Playposition.NUM_POSITIONS; i++) {
            cur = rb.getPosition(i);
            min = cur.getMinteam();
            max = cur.getMaxteam();
            num = roster.getPosCount(i);
            if (num < min) {
                error = "Too few " + cur.getName() + "s.";
                return false;
            }
            if (num > max) {
                error = "Too many " + cur.getName() + "s. ";
                return false;
            }
        }
        return true;
    }
    
    /**
     * Assigns the roster.  After this button is pressed.  Can only be done if roster is validated first.
     * canAssign *MUST* be true for this method to be able to be called
     * @return 
     */
    public boolean assignRoster() {
        for (int i = 0; i < roster.rosterCount(); i++) {
            RosterNode r = roster.getNode(i);
            if(!rb.updatePlayerActive(r.getPlayerId(), roster.getTeamid(), r.getActiveShort()))
                return false;
        }
        return true;
    }
    
    /**
     * Toggles whether a player is active or benched
     * @param i : Position of the player roster list
     */
    public void toggleActive(int i) {
        if (!rb.isPlayerInTrade(roster.getNode(i).getPlayerId()))
            roster.toggleActive(i);
    }
    
    /**
     * Returns the name of a player
     * @param i : Position of the player on the roster list
     * @return : The Player's name
     */
    public String getName(int i) {
        int pid = roster.getPlayerId(i);
        return rb.getPlayerById(pid).getName();
    }
    
    /**
     * Returns the position of a player
     * @param i : Position of the player on a roster list
     * @return : The abbreviation of the position.
     */
    public String getPos(int i) {
        int posId = roster.getPos(i);
        return rb.getPosition(posId).getAbbr();
    }
    
    public String getTeamName() {
        return team.getName();
    }
    
    
    
}
